﻿using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Text.Json.Serialization;

namespace GloboTicketAdmin.API.Common
{
    /// <summary>
    /// 通用API响应模型
    /// </summary>
    public class ApiResponse<T>
    {
        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 业务状态码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 提示消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 响应数据
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 服务器时间戳
        /// </summary>
        public DateTime Timestamp { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 错误详情（仅在出错时返回）
        /// </summary>
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public Dictionary<string, string> Errors { get; set; }

        /// <summary>
        /// 成功响应
        /// </summary>
        /// <param name="data">返回的数据</param>
        /// <param name="message">成功消息</param>
        /// <param name="code">业务状态码</param>
        /// <returns>API响应对象</returns>
        public static ApiResponse<T> SuccessResult(T data = default, string message = "操作成功", int code = 200)
        {
            return new ApiResponse<T>
            {
                Success = true,
                Code = code,
                Message = message,
                Data = data
            };
        }

        /// <summary>
        /// 错误响应
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="code">业务状态码</param>
        /// <param name="errors">错误详情</param>
        /// <returns>API响应对象</returns>
        public static ApiResponse<T> ErrorResult(string message = "操作失败", int code = 500, Dictionary<string, string> errors = null)
        {
            return new ApiResponse<T>
            {
                Success = false,
                Code = code,
                Message = message,
                Errors = errors
            };
        }

        /// <summary>
        /// 从验证失败结果创建错误响应
        /// </summary>
        /// <param name="modelState">验证状态</param>
        /// <returns>API响应对象</returns>
        public static ApiResponse<T> FromValidationError(ModelStateDictionary modelState)
        {
            var errors = modelState.Where(x => x.Value.Errors.Any())
                .ToDictionary(
                    kvp => kvp.Key,
                    kvp => string.Join("; ", kvp.Value.Errors.Select(e => e.ErrorMessage))
                );

            return ErrorResult("数据验证失败", 400, errors);
        }
    }

    /// <summary>
    /// 无数据的API响应（非泛型版本）
    /// </summary>
    public class ApiResponse : ApiResponse<object>
    {
        /// <summary>
        /// 成功响应（无数据）
        /// </summary>
        public static ApiResponse SuccessResult(string message = "操作成功", int code = 200)
        {
            return new ApiResponse
            {
                Success = true,
                Code = code,
                Message = message
            };
        }

        /// <summary>
        /// 错误响应（无数据）
        /// </summary>
        public new static ApiResponse ErrorResult(string message = "操作失败", int code = 500, Dictionary<string, string> errors = null)
        {
            return new ApiResponse
            {
                Success = false,
                Code = code,
                Message = message,
                Errors = errors
            };
        }
    }
}
